﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using SoucreGame.GLO;
using SoucreGame.BUS;
using SoucreGame.BUS.CBusiTower;
using SoucreGame.BUS.CBusiSkill;

namespace SoucreGame.GUI
{
    class guiMapGamePlay
    {
         // màn hình vào game
        Texture2D guiMapgame;  // hinh anh lam nen
        Texture2D cmdNutCaiDat;
        Texture2D cmdNutTamDung;
        Texture2D guiBangThongTin;

        Texture2D cmdNhaLinh;
        Texture2D cmdThapCung;
        Texture2D cmdThapDoc;
        Texture2D cmdThapPhep;
        Texture2D cmdThapPhongKhong;
        Texture2D cmdThapPhongThu;
        Texture2D cmdTruPhao;

        //Texture2D cmdIconNhaLinh;
        //Texture2D cmdIconThapCung;
        //Texture2D cmdIconThapDoc;
        //Texture2D cmdIconThapPhep;
        //Texture2D cmdIconThapPhongKhong;
        //Texture2D cmdIconThapPhongThu;
        //Texture2D cmdIconTruPhao;

        Texture2D cmdBomNo;
        Texture2D cmdHoiMau;
        Texture2D cmdLamCham;
        Texture2D cmdNoKiem;
        Texture2D cmdSamSet;
        Texture2D cmdSuTuHong;
        Texture2D cmdThienThach;
        Texture2D cmdTroi;
        Texture2D cmdXoayNuoc;

        Texture2D guiHongTam;
        

        CglobalFuntion.KyHieuNhanVat kyHieuNV;
        CBusiMapChoi _mapChoi;

        Dictionary<string, Rectangle> dsToaDoGiaoDien;
        ContentManager content;
        GameTime gameTime;
        SpriteBatch spriteBatch;

        public guiMapGamePlay(ContentManager _content, GameTime _gameTime, SpriteBatch _spritBatch)
        {
            content = _content;
            gameTime = _gameTime;
            spriteBatch = _spritBatch;

            if (content != null)
            {
                guiMapgame = content.Load<Texture2D>("GUII\\guiMapGamePlay");
                cmdNutCaiDat = content.Load<Texture2D>("GUII\\cmdNutCaiDat");
                cmdNutTamDung = content.Load<Texture2D>("GUII\\cmdNutTamDung");
                guiBangThongTin = content.Load<Texture2D>("GUII\\guiBangThongTin");

                cmdNhaLinh = content.Load<Texture2D>("GUII\\cmdNhaLinh");
                cmdThapCung = content.Load<Texture2D>("GUII\\cmdThapCung");
                cmdThapDoc = content.Load<Texture2D>("GUII\\cmdThapDoc");
                cmdThapPhep = content.Load<Texture2D>("GUII\\cmdThapPhep");
                cmdThapPhongKhong = content.Load<Texture2D>("GUII\\cmdThapPhongKhong");
                cmdThapPhongThu = content.Load<Texture2D>("GUII\\cmdThapPhongThu");
                cmdTruPhao = content.Load<Texture2D>("GUII\\cmdTruPhao");

                //cmdIconNhaLinh = content.Load<Texture2D>("GUII\\cmdIconNhaLinh");
                //cmdIconThapCung = content.Load<Texture2D>("GUII\\cmdIconThapCung");
                //cmdIconThapDoc = content.Load<Texture2D>("GUII\\cmdIconThapDoc");
                //cmdIconThapPhep = content.Load<Texture2D>("GUII\\cmdIconThapPhep");
                //cmdIconThapPhongKhong = content.Load<Texture2D>("GUII\\cmdIconThapPhongKhong");
                //cmdIconThapPhongThu = content.Load<Texture2D>("GUII\\cmdIconThapPhongThu");
                //cmdIconTruPhao = content.Load<Texture2D>("GUII\\cmdIconTruPhao");

                cmdBomNo = content.Load<Texture2D>("GUII\\cmdBomNo");
                cmdHoiMau = content.Load<Texture2D>("GUII\\cmdHoiMau");
                cmdLamCham = content.Load<Texture2D>("GUII\\cmdLamCham");
                cmdNoKiem = content.Load<Texture2D>("GUII\\cmdNoKiem");
                cmdSamSet = content.Load<Texture2D>("GUII\\cmdSamSet");
                cmdSuTuHong = content.Load<Texture2D>("GUII\\cmdSuTuHong");
                cmdThienThach = content.Load<Texture2D>("GUII\\cmdThienThach");
                cmdTroi = content.Load<Texture2D>("GUII\\cmdTroi");
                cmdXoayNuoc = content.Load<Texture2D>("GUII\\cmdXoayNuoc");

                guiHongTam = content.Load<Texture2D>("GUII\\cmdHongTam");                

                dsToaDoGiaoDien = content.Load<Dictionary<string, Rectangle>>("GUII\\guiViTri");

                _mapChoi = new CBusiMapChoi();
                _mapChoi.QuanTa = new List<CBusiNhanVat>();
                _mapChoi.QuanDich = new List<CBusiNhanVat>();
                _mapChoi.KyNang = new List<CBusiKyNang>();
            }
        }

        Rectangle rec = new Rectangle(-1, -1, -1, -1);
        public void Update(GameTime gameTime)
        {
            CGlobalvariable.trangthaitruoc = CGlobalvariable.trangthaihientai;
            CGlobalvariable.trangthaihientai = Mouse.GetState();

            if (CGlobalvariable.trangthaitruoc.LeftButton == ButtonState.Released && CGlobalvariable.trangthaihientai.LeftButton == ButtonState.Pressed)
            {                
                if (dsToaDoGiaoDien["cmdBomNo"].Contains(new Point(CGlobalvariable.trangthaihientai.X, CGlobalvariable.trangthaihientai.Y)))
                {
                    CGlobalvariable.isMove = true;
                    kyHieuNV = CglobalFuntion.KyHieuNhanVat.KyNangBomNo;
                }

                if (dsToaDoGiaoDien["cmdHoiMau"].Contains(new Point(CGlobalvariable.trangthaihientai.X, CGlobalvariable.trangthaihientai.Y)))
                {
                    CGlobalvariable.isMove = true;
                    kyHieuNV = CglobalFuntion.KyHieuNhanVat.KyNangHoiMau;
                }

                if (dsToaDoGiaoDien["cmdLamCham"].Contains(new Point(CGlobalvariable.trangthaihientai.X, CGlobalvariable.trangthaihientai.Y)))
                {
                    CGlobalvariable.isMove = true;
                    kyHieuNV = CglobalFuntion.KyHieuNhanVat.KyNangLamCham;
                }

                if (dsToaDoGiaoDien["cmdNoKiem"].Contains(new Point(CGlobalvariable.trangthaihientai.X, CGlobalvariable.trangthaihientai.Y)))
                {
                    CGlobalvariable.isMove = true;
                    kyHieuNV = CglobalFuntion.KyHieuNhanVat.KyNangNoKiem;
                }

                if (dsToaDoGiaoDien["cmdSamSet"].Contains(new Point(CGlobalvariable.trangthaihientai.X, CGlobalvariable.trangthaihientai.Y)))
                {
                    CGlobalvariable.isMove = true;
                    kyHieuNV = CglobalFuntion.KyHieuNhanVat.KyNangSamSet;
                }

                if (dsToaDoGiaoDien["cmdSuTuHong"].Contains(new Point(CGlobalvariable.trangthaihientai.X, CGlobalvariable.trangthaihientai.Y)))
                {
                    CGlobalvariable.isMove = true;
                    kyHieuNV = CglobalFuntion.KyHieuNhanVat.KyNangSuTuHong;
                }

                if (dsToaDoGiaoDien["cmdThienThach"].Contains(new Point(CGlobalvariable.trangthaihientai.X, CGlobalvariable.trangthaihientai.Y)))
                {
                    CGlobalvariable.isMove = true;
                    kyHieuNV = CglobalFuntion.KyHieuNhanVat.KyNangThienThach;
                }

                if (dsToaDoGiaoDien["cmdTroi"].Contains(new Point(CGlobalvariable.trangthaihientai.X, CGlobalvariable.trangthaihientai.Y)))
                {
                    CGlobalvariable.isMove = true;
                    kyHieuNV = CglobalFuntion.KyHieuNhanVat.KyNangTroi;
                }

                if (dsToaDoGiaoDien["cmdXoayNuoc"].Contains(new Point(CGlobalvariable.trangthaihientai.X, CGlobalvariable.trangthaihientai.Y)))
                {
                    CGlobalvariable.isMove = true;
                    kyHieuNV = CglobalFuntion.KyHieuNhanVat.KyNangXoayNuoc;
                }
                

                CGlobalvariable.posActor = new Vector2(CGlobalvariable.trangthaihientai.X, CGlobalvariable.trangthaihientai.Y);
            }

            if (CGlobalvariable.isMove== true && CGlobalvariable.trangthaitruoc.LeftButton == ButtonState.Pressed && CGlobalvariable.trangthaihientai.LeftButton == ButtonState.Released)
            {
                CGlobalvariable.isMove = false;
                Rectangle rec = CglobalFuntion.ToaDo(CGlobalvariable.trangthaihientai.X, CGlobalvariable.trangthaihientai.Y);
                CGlobalvariable.posActor = new Vector2(rec.Width, rec.Height);

                if (kyHieuNV == CglobalFuntion.KyHieuNhanVat.KyNangBomNo)
                {
                    _mapChoi.kyNang.Add(new CBusiBomNo(content, gameTime, spriteBatch));                    
                }

                if (kyHieuNV == CglobalFuntion.KyHieuNhanVat.KyNangHoiMau)
                {                    
                    _mapChoi.kyNang.Add(new CBusiHoiMau(content, gameTime, spriteBatch));
                }

                if (kyHieuNV == CglobalFuntion.KyHieuNhanVat.KyNangLamCham)
                {
                    _mapChoi.kyNang.Add(new CBusiLamCham(content, gameTime, spriteBatch));
                }
                if (kyHieuNV == CglobalFuntion.KyHieuNhanVat.KyNangNoKiem)
                {
                    _mapChoi.kyNang.Add(new CBusiNoKiem(content, gameTime, spriteBatch));
                }

                if (kyHieuNV == CglobalFuntion.KyHieuNhanVat.KyNangSamSet)
                {
                    _mapChoi.kyNang.Add(new CBusiSamSet(content, gameTime, spriteBatch));
                }

                if (kyHieuNV == CglobalFuntion.KyHieuNhanVat.KyNangSuTuHong)
                {
                    _mapChoi.kyNang.Add(new CBusiSuTuHong(content, gameTime, spriteBatch));
                }

                if (kyHieuNV == CglobalFuntion.KyHieuNhanVat.KyNangThienThach)
                {
                    _mapChoi.kyNang.Add(new CBusiThienThach(content, gameTime, spriteBatch));
                }

                if (kyHieuNV == CglobalFuntion.KyHieuNhanVat.KyNangTroi)
                {
                    _mapChoi.kyNang.Add(new CBusiTroi(content, gameTime, spriteBatch));
                }

                if (kyHieuNV == CglobalFuntion.KyHieuNhanVat.KyNangXoayNuoc)
                {
                    _mapChoi.kyNang.Add(new CBusiXoayNuoc(content, gameTime, spriteBatch));
                }
            }
            _mapChoi.Update(gameTime);
        }
        
        public void Draw(GameTime gameTime)
        {
            this.spriteBatch.Draw(guiMapgame, Vector2.Zero, Color.White);
            this.spriteBatch.Draw(cmdNutCaiDat, new Vector2(dsToaDoGiaoDien["cmdNutCaiDat"].X, dsToaDoGiaoDien["cmdNutCaiDat"].Y), Color.White);
            this.spriteBatch.Draw(cmdNutTamDung, new Vector2(dsToaDoGiaoDien["cmdNutTamDung"].X, dsToaDoGiaoDien["cmdNutTamDung"].Y), Color.White);
            this.spriteBatch.Draw(guiBangThongTin, new Vector2(dsToaDoGiaoDien["guiBangThongTin"].X, dsToaDoGiaoDien["guiBangThongTin"].Y), Color.White);

            this.spriteBatch.Draw(cmdNhaLinh, new Vector2(dsToaDoGiaoDien["cmdNhaLinh"].X, dsToaDoGiaoDien["cmdNhaLinh"].Y), Color.White);
            this.spriteBatch.Draw(cmdThapCung, new Vector2(dsToaDoGiaoDien["cmdThapCung"].X, dsToaDoGiaoDien["cmdThapCung"].Y), Color.White);
            this.spriteBatch.Draw(cmdThapDoc, new Vector2(dsToaDoGiaoDien["cmdThapDoc"].X, dsToaDoGiaoDien["cmdThapDoc"].Y), Color.White);
            this.spriteBatch.Draw(cmdThapPhep, new Vector2(dsToaDoGiaoDien["cmdThapPhep"].X, dsToaDoGiaoDien["cmdThapPhep"].Y), Color.White);
            this.spriteBatch.Draw(cmdThapPhongKhong, new Vector2(dsToaDoGiaoDien["cmdThapPhongKhong"].X, dsToaDoGiaoDien["cmdThapPhongKhong"].Y), Color.White);
            this.spriteBatch.Draw(cmdThapPhongThu, new Vector2(dsToaDoGiaoDien["cmdThapPhongThu"].X, dsToaDoGiaoDien["cmdThapPhongThu"].Y), Color.White);
            this.spriteBatch.Draw(cmdTruPhao, new Vector2(dsToaDoGiaoDien["cmdTruPhao"].X, dsToaDoGiaoDien["cmdTruPhao"].Y), Color.White);

            //this.spriteBatch.Draw(cmdIconNhaLinh, new Vector2(dsToaDoGiaoDien["cmdIconNhaLinh"].X, dsToaDoGiaoDien["cmdIconNhaLinh"].Y), Color.White);
            //this.spriteBatch.Draw(cmdIconThapCung, new Vector2(dsToaDoGiaoDien["cmdIconThapCung"].X, dsToaDoGiaoDien["cmdIconThapCung"].Y), Color.White);
            //this.spriteBatch.Draw(cmdIconThapDoc, new Vector2(dsToaDoGiaoDien["cmdIconThapDoc"].X, dsToaDoGiaoDien["cmdIconThapDoc"].Y), Color.White);
            //this.spriteBatch.Draw(cmdIconThapPhep, new Vector2(dsToaDoGiaoDien["cmdIconThapPhep"].X, dsToaDoGiaoDien["cmdIconThapPhep"].Y), Color.White);
            //this.spriteBatch.Draw(cmdIconThapPhongKhong, new Vector2(dsToaDoGiaoDien["cmdIconThapPhongKhong"].X, dsToaDoGiaoDien["cmdIconThapPhongKhong"].Y), Color.White);
            //this.spriteBatch.Draw(cmdIconThapPhongThu, new Vector2(dsToaDoGiaoDien["cmdIconThapPhongThu"].X, dsToaDoGiaoDien["cmdIconThapPhongThu"].Y), Color.White);
            //this.spriteBatch.Draw(cmdIconTruPhao, new Vector2(dsToaDoGiaoDien["cmdIconTruPhao"].X, dsToaDoGiaoDien["cmdIconTruPhao"].Y), Color.White);

            if (CGlobalvariable.isMove == true)
            {
                if (kyHieuNV == CglobalFuntion.KyHieuNhanVat.KyNangBomNo)
                    this.spriteBatch.Draw(guiHongTam, new Vector2(CGlobalvariable.trangthaihientai.X - 41, CGlobalvariable.trangthaihientai.Y - 41), Color.White);
                if (kyHieuNV == CglobalFuntion.KyHieuNhanVat.KyNangHoiMau)
                    this.spriteBatch.Draw(guiHongTam, new Vector2(CGlobalvariable.trangthaihientai.X - 41, CGlobalvariable.trangthaihientai.Y - 41), Color.White);
                if (kyHieuNV == CglobalFuntion.KyHieuNhanVat.KyNangLamCham)
                    this.spriteBatch.Draw(guiHongTam, new Vector2(CGlobalvariable.trangthaihientai.X - 41, CGlobalvariable.trangthaihientai.Y - 41), Color.White);
                if (kyHieuNV == CglobalFuntion.KyHieuNhanVat.KyNangNoKiem)
                    this.spriteBatch.Draw(guiHongTam, new Vector2(CGlobalvariable.trangthaihientai.X - 41, CGlobalvariable.trangthaihientai.Y - 41), Color.White);
                if (kyHieuNV == CglobalFuntion.KyHieuNhanVat.KyNangSamSet)
                    this.spriteBatch.Draw(guiHongTam, new Vector2(CGlobalvariable.trangthaihientai.X - 41, CGlobalvariable.trangthaihientai.Y - 41), Color.White);
                if (kyHieuNV == CglobalFuntion.KyHieuNhanVat.KyNangSuTuHong)
                    this.spriteBatch.Draw(guiHongTam, new Vector2(CGlobalvariable.trangthaihientai.X - 41, CGlobalvariable.trangthaihientai.Y - 41), Color.White);
                if (kyHieuNV == CglobalFuntion.KyHieuNhanVat.KyNangThienThach)
                    this.spriteBatch.Draw(guiHongTam, new Vector2(CGlobalvariable.trangthaihientai.X - 41, CGlobalvariable.trangthaihientai.Y - 41), Color.White);
                if (kyHieuNV == CglobalFuntion.KyHieuNhanVat.KyNangTroi)
                    this.spriteBatch.Draw(guiHongTam, new Vector2(CGlobalvariable.trangthaihientai.X - 41, CGlobalvariable.trangthaihientai.Y - 41), Color.White);
                if (kyHieuNV == CglobalFuntion.KyHieuNhanVat.KyNangXoayNuoc)
                    this.spriteBatch.Draw(guiHongTam, new Vector2(CGlobalvariable.trangthaihientai.X - 41, CGlobalvariable.trangthaihientai.Y - 41), Color.White);

            }
            
            
            this.spriteBatch.Draw(cmdBomNo, new Vector2(dsToaDoGiaoDien["cmdBomNo"].X, dsToaDoGiaoDien["cmdBomNo"].Y), Color.White);
            this.spriteBatch.Draw(cmdHoiMau, new Vector2(dsToaDoGiaoDien["cmdHoiMau"].X, dsToaDoGiaoDien["cmdHoiMau"].Y), Color.White);
            this.spriteBatch.Draw(cmdLamCham, new Vector2(dsToaDoGiaoDien["cmdLamCham"].X, dsToaDoGiaoDien["cmdLamCham"].Y), Color.White);
            this.spriteBatch.Draw(cmdNoKiem, new Vector2(dsToaDoGiaoDien["cmdNoKiem"].X, dsToaDoGiaoDien["cmdNoKiem"].Y), Color.White);
            this.spriteBatch.Draw(cmdSamSet, new Vector2(dsToaDoGiaoDien["cmdSamSet"].X, dsToaDoGiaoDien["cmdSamSet"].Y), Color.White);
            this.spriteBatch.Draw(cmdSuTuHong, new Vector2(dsToaDoGiaoDien["cmdSuTuHong"].X, dsToaDoGiaoDien["cmdSuTuHong"].Y), Color.White);
            this.spriteBatch.Draw(cmdThienThach, new Vector2(dsToaDoGiaoDien["cmdThienThach"].X, dsToaDoGiaoDien["cmdThienThach"].Y), Color.White);
            this.spriteBatch.Draw(cmdTroi, new Vector2(dsToaDoGiaoDien["cmdTroi"].X, dsToaDoGiaoDien["cmdTroi"].Y), Color.White);
            this.spriteBatch.Draw(cmdXoayNuoc, new Vector2(dsToaDoGiaoDien["cmdXoayNuoc"].X, dsToaDoGiaoDien["cmdXoayNuoc"].Y), Color.White);

            _mapChoi.Draw(gameTime);
        }
    }
}
